package houxb.cn;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.Key;
import java.util.Base64;

public class FileEncryptor {
    // AES密钥（示例，实际应使用密钥管理系统）
    private static final String SECRET_KEY = "My32CharUltraSecuredKeyAa@123456";

    public static void main(String[] args) throws Exception {
        String inputFile = "原始文件.txt";
        String outputFile = "加密后.bin";
        // 读取文件为字节数组
        byte[] fileBytes = Files.readAllBytes(Paths.get(inputFile));

        // AES加密
        Key key = new SecretKeySpec(SECRET_KEY.getBytes(), "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] encryptedBytes = cipher.doFinal(fileBytes);

        // 转换为Base64编码的二进制字符串（避免乱码）
        String encodedBinary = Base64.getEncoder().encodeToString(encryptedBytes);
        // 写入加密文件
        Files.write(Paths.get(outputFile), encodedBinary.getBytes());
    }
}
